﻿@model DHQGHN.PM0.Models.Admin.UserInGroupModel
@{
    var lstUser = ViewBag.lstUser;
    var columnsMenu = 2;
    // var rowsMenu = lstUser.Items.Count / columnsMenu;
    var rowsMenu = lstUser.Items.Count % 2 == 0 ? (lstUser.Items.Count / columnsMenu) : ((lstUser.Items.Count + 1) / columnsMenu);
    int index = 0;
}


@using (
    Html.BeginForm(
        actionName: "actionCreateOrUpdateUserInGroup"
        , controllerName: "UserInGroup"
        , routeValues: new { }
        , method: FormMethod.Post
        , htmlAttributes: new { @id = "user-in-group-form", @class = "form-inline", @role = "form", @style = "width: 100%;" }
        )
    )
{
    <div class="modal-body min-width">
        <div class="form-group">
            @Html.AntiForgeryToken()
            @Html.HiddenFor(m => m.UserInGroupId)
            <div>
                @Html.LabelFor(m => m.GroupId, new { @class = "labels", style = "width:100px;" })<span class="field-validation-error"> * </span>
                @Html.DropDownListFor(m => m.GroupId, new SelectList(ViewBag.lstGroup, "value", "text", 0), new { @class = "SelectBoxContent", @placeholder = "", @style = "width: 260px !important;", @disabled = "disabled" })
                @Html.ValidationMessageFor(m => m.GroupId, null, new { @class = "Validation_Error", @style = "margin-left:120px" })
                <input type="text" class="TextBoxContent" style="width: 10% !important" id="input-appname" name="appname" placeholder="Nhập tên">
                <button class="btn btn-primary" type="button" onclick="return btnSearchClick();">
                    <span class="glyphicon glyphicon-search"></span>Tìm kiếm
                </button>
            </div>

            <div>
                @Html.Label("Người dùng")<span class="field-validation-error">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;* </span>
                &nbsp;Bạn có thể gán @lstUser.Items.Count người vào nhóm này !
                @* @Html.LabelFor(m => m.Username, new { @class = "labels", style = "width:100px;" })<span> &nbsp; </span>*@
                <div style="max-height: 200px; overflow: auto; resize: none;">
                    @if (!(lstUser.Items.Count == 0 || lstUser == null))
                    {
                        <table class="tbl-list" style="margin-left: 115px; width:80%">
                            @for (int i = 0; i < rowsMenu; i++)
                            {
                                <tr class="test-class">
                                    @for (int j = 0; j < columnsMenu; j++)
                                    {
                                        index = columnsMenu * i + j;
                                        <td style="width:40%" class="td-onclick"></td>
                                        if (index >= lstUser.Items.Count) { break; }
                                        
                                        <td style="width:40%" class="td-onclick">
                                            <input type="checkbox" id="checkboxUserChecked" name="@lstUser.Items[index].Value" />
                                            &nbsp;&nbsp;&nbsp;@lstUser.Items[index].Text
                                        </td>
                                    }
                                </tr>
                            }
                        </table>
                    }
                    else
                    {
                        <span class="field-validation-error" style="margin-left:112px">Nhóm đã gán hết người dùng</span>
                    }
                </div>
                <br />
            </div>
        </div>
    </div>
}

<div id="overlay" class="web_dialog_overlay"></div>
@Scripts.Render("~/bundles/jqueryval")

<script>
    @functions{
            public string GetAntiForgeryToken()
            {
                string cookieToken, formToken;
                AntiForgery.GetTokens(null, out cookieToken, out formToken);
                return cookieToken + "," + formToken;
            }
        }
</script>
<script>
    var $userInGroupForm = $("#user-in-group-form");
    function save() {
        var result = false;
        var listID = getListUserID();
        if ($userInGroupForm.valid() && $('#GroupId').val().trim() > 0) {
            $("#GroupId").prop("disabled", false);
            $.ajax({
                contentType: "application/json",
                type: "POST",
                url: $userInGroupForm.attr('action'),
                data: JSON.stringify({ GroupId: $('#GroupId').val().trim(), listID: listID }),
                dataType: 'json',//chú ý là form đặc biệt sthì phải điền trường này
                async: false,
                headers: {
                    'VerificationToken': '@GetAntiForgeryToken()'
                },
                success: function (response) {
                    if (response != null && response.status == 200) {
                        window.notice(response.result, window.notice_success);
                        OnCriteria(crrPageIndex);
                        result = true;
                    } else {
                        window.notice(response.result, window.notice_error);
                        $("#GroupId").prop("disabled", true);
                    }
                }
            });
        }
        return result;

    }

    $('.td-onclick').click(function () {
        $(this).find(':checkbox').click();
    });

    $(":checkbox").click(function (e) {
        e.stopPropagation();
    });

    function getListUserID() {
        var values = [];
        $('#checkboxUserChecked input:checked').each(function () {
            values.push(this.name);
        });
        return values;
    }

    //$('#select_all').click(function (event) {
    //    if (this.checked) {
    //        // Iterate each checkbox
    //        $(':checkbox').each(function () {
    //            this.checked = true;
    //        });
    //    }
    //    else {
    //        $(':checkbox').each(function () {
    //            this.checked = false;
    //        });
    //    }
    //});

    function btnSearchClick() {
        OnCriterianame(0);
    }

    var crrPageIndex;
    function OnCriterianame(pageIndex) {
        var appname = $('#input-appname');
        crrPageIndex = pageIndex;
        appname = appname != null ? appname.val().trim() : "";
        $.ajax({
            dataType: "html",
            type: "POST",
            //type: "GET",
            url: '/UserInGroup/actionSearchname',
            contentType: "application/json",
            //data: { key: ctrSearch, departmentId: ctrdepartmentId, pageIndex: pageIndex },
            data: JSON.stringify({ SeatchParam: appname,groupID:$('#GroupId').val().trim() }),
            success: function (response) {
                var $catlist = $('#app-list');

                if ($catlist == null || $catlist.length == 0) {
                    window.notice('@*@Resources.Localization.MsgItemNotFound*@', window.notice_error);
                }
                else {
                    $catlist.html(response);
                }
            }
        });
        setTimeout(function () { $('#search_form').find('input:text:visible:first').focus(); }, 500);
    }

</script>
